/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.operativa;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.operativa.exception.RemisionException;
import co.gov.medellin.sdep.sisdep.operativa.exception.RemisionRuntimeException;
import co.gov.medellin.sdep.sisdep.operativa.service.IRemisionService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.model.Remision;

/**
 * @author Usuario
 * 
 */
public class RemisionController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public RemisionController() {
		this.remisionListModel = new ListDataModel();
	}

	/** bean para el manejo del formulario de busqueda Remision */
	public Remision remisionSearch = new Remision();

	/** Objeto contenedor del listado de Remision luego de una consulta. */
	private DataModel remisionListModel;

	/** bean para el manejo del formulario de creacion/edicion. Remision */
	private Remision remision = new Remision();

	/** instancia de servicio remisionService */
	private IRemisionService remisionService;

	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchRemisionByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getRemisionService().load(1, 10,
					this.remisionSearch);
			this.remisionListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.remisionListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.remisionListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.REMISION_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return String
	 */
	public String searchRemisionByRadicado() {
		String msg = "";
		try {
			List valores = null;
			valores = getRemisionService().loadByRadicado(1, 10,
					this.remisionSearch);
			this.remisionListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.remisionListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.remisionListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.REMISION_SEARCH_VIEW;
	}
	
	public String searchRemisionByRadicado(IRemisionService service) {
		String msg = "";
		try {
			List valores = null;
			valores = service.loadByRadicado(1, 10,
					this.remisionSearch);
			this.remisionListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.remisionListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.remisionListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.REMISION_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.remision.getId().longValue() == 0L) {
				remision.setIdComparendoEntidad(new Long(remision.getIdComparendoEntidadStr()));
				getRemisionService().insert(this.remision);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (RemisionException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (RemisionRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- remision " + msg, null, null);
		return AppConstants.REMISION_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.remision.getId().longValue() != 0L) {
				remision.setIdComparendoEntidad(new Long(remision.getIdComparendoEntidadStr()));
				getRemisionService().update(this.remision);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (RemisionException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (RemisionRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Valor Tipo Maestra " + msg, null, null);
		return AppConstants.REMISION_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createRemision() {
		remision = new Remision();
		remision.setId(new Long(0L));
		remision.setCodigo("");
		remision.setIdComparendo(remisionSearch.getIdComparendo());
		//remision.setValor("");
		//remision.setActivo("N");
		return AppConstants.REMISION_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateRemision() {
		Map params = getExternalContext().getRequestParameterMap();
		String remisionDataIndex = (String) params
				.get("remisionDataIndex");
		this.remision = (Remision) ((List) this.remisionListModel
				.getWrappedData())
				.get(Integer.parseInt(remisionDataIndex));
		return AppConstants.REMISION_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String cancelRemision() {
		return AppConstants.REMISION_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public Remision getRemisionSearch() {
		return remisionSearch;
	}

	/**
	 * 
	 * @param remisionSearch
	 */
	public void setremisionSearch(Remision remisionSearch) {
		this.remisionSearch = remisionSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getremisionListModel() {
		return remisionListModel;
	}

	/**
	 * 
	 * @param remisionListModel
	 */
	public void setremisionListModel(DataModel remisionListModel) {
		this.remisionListModel = remisionListModel;
	}

	/**
	 * 
	 * @return
	 */
	public Remision getRemision() {
		return remision;
	}

	/**
	 * 
	 * @param remision
	 */
	public void setremision(Remision remision) {
		this.remision = remision;
	}

	/**
	 * obtencion del servicio remisionService
	 * 
	 * @return
	 */
	public IRemisionService getRemisionService() {
		return remisionService;
	}

	/**
	 * establecimiento del servicio remisionService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param remisionService
	 */
	public void setRemisionService(
			IRemisionService remisionService) {
		this.remisionService = remisionService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
